"""
Case Type   : 小型化
Case Name   : 安装时主节点使用replconninfo1通道备节点使用replconninfo2通道
Create At   : 2024/10/31
Owner       : ningyali
Description :
    1.获取安装包并解压
    2.安装时主节点使用replconninfo1通道备节点使用replconninfo2通道
    3.检查主备通路是否正常
    4.清理数据环境
Expect      :
    1.成功
    2.成功
    3.正常
    4.清理完成
History     :
"""
import os
import time
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(Primary_SH.get_node_num() < 2, '需至少两个环境，否则不执行')
class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'))
        self.user = None
        self.port = None
        self.assert_msg = f'start datanode success'
        self.db_name = 'db_lite_install_59'

    def test_upgrade(self):
        text = '----prestep1:获取可用用户和端口----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(
            *self.root_nodes)
        self.log.info(f'self.user:{self.user}, self.port:{self.port}')
        for node in self.user_nodes:
            node.ssh_user = self.user
            node.ssh_password = macro.COMMON_PASSWD

        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, *self.root_nodes)

        text = '----step1:主备获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        for node in self.root_nodes:
            res = self.com_install.tar_pkg(node)
            self.assertIn('install.sh', res, f'{node.ssh_host}-执行失败: {text}')

        text = '----step2:安装时主节点使用replconninfo1通道备节点使用replconninfo2通道;' \
               'expect:执行成功----'
        self.log.info(text)
        script_path = os.path.join(self.pkg_path, 'lite_pkg')
        common_cmd = f'cd {script_path} && ' \
                     f'echo {macro.COMMON_PASSWD}|sh ./install.sh ' \
                     f'-D {self.data_path} -R {self.app_path} ' \
                     f'-C "port={self.port}" ' \
                     f'-C "modify_initial_password=false" ' \
                     f'--start'
        pri_cmd = f'--mode primary -C "replconninfo1 = \'' \
                  f'localhost={self.user_nodes[0].ssh_host} ' \
                  f'localport={int(self.port) + 1} ' \
                  f'remotehost={self.user_nodes[1].ssh_host} ' \
                  f'remoteport={int(self.port) + 1}\'"'
        sta_cmd = f'--mode standby -C "replconninfo2 = \'' \
                  f'localhost={self.user_nodes[1].ssh_host} ' \
                  f'localport={int(self.port) + 1} ' \
                  f'remotehost={self.user_nodes[0].ssh_host} ' \
                  f'remoteport={int(self.port) + 1}\'"'
        self.log.info(f'{common_cmd} {pri_cmd}')
        pri_res = self.user_nodes[0].sh(f'{common_cmd} {pri_cmd}').result()
        self.log.info(pri_res)
        self.log.info('主节点安装完成\n\n')
        self.assertIn(self.assert_msg, pri_res, f'执行失败: {text}')

        self.log.info(f'{common_cmd} {sta_cmd}')
        sta_res = self.user_nodes[1].sh(f'{common_cmd} {sta_cmd}').result()
        self.log.info(sta_res)
        self.log.info('备节点安装完成\n\n')
        self.assertIn(self.assert_msg, sta_res, f'执行失败: {text}')

        text = '----step3:检查主备通路是否正常;expect:执行成功----'
        self.log.info(text)
        text = '----step3.1:主机创建数据库;expect:成功----'
        self.log.info(text)
        cmd = f'gsql -d postgres -p {self.port} -c "' \
              f'drop database if exists {self.db_name};' \
              f'create database {self.db_name}";'
        self.log.info(cmd)
        pri_res = self.user_nodes[0].sh(cmd).result()
        self.log.info(pri_res)
        self.assertIn('CREATE DATABASE', pri_res, f'执行失败: {text}')

        text = '----step3.2:备机查询数据库;expect:成功----'
        self.log.info(text)
        time.sleep(5)
        cmd = f'gsql -d postgres -p {self.port} -c "' \
              f'select * from pg_database where datname=\'{self.db_name}\';"'
        self.log.info(cmd)
        sta_res = self.user_nodes[1].sh(cmd).result()
        self.log.info(sta_res)
        self.assertIn('1 row', sta_res, f'执行失败: {text}')

    def tearDown(self):
        text = '----step4:清理环境;expect:成功----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(*self.user_nodes)
        self.com_uninstall.clear_env(*self.root_nodes)
        self.assertIn('success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
